﻿using System;

using System.Text;
using IAM.Interface.Model.Interface;
using IAM.Interface.Model.ENUM;
using System.Web;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 请求包头
    /// </summary>
    [Serializable]
    public class RequestData : AbstractConvertObject,IConvertXml
    {
        #region 属性
        private string _transactionid = "";
        private DateTime _requesttime = System.DateTime.Now;
        private string _appname = "";
        private string _token = "";
        private ActionType _actiontype = ActionType.AUDIT_FETCH;
        /// <summary>
        /// 发起请求的事务ID
        /// 当收到服务器的Response消息时需要比对该ID。
        /// 该ID采用Guid格式
        /// </summary>
        public string TransactionID
        {
            set
            {
                _transactionid = value; 
            }
            get
            {
                return _transactionid;
            }
        }
        /// <summary>
        /// 请求时间
        /// </summary>
        public DateTime RequestTime
        {
            set
            {
                _requesttime = value;
            }
            get
            {
                return _requesttime;
            }
        }
        /// <summary>
        /// 集成系统名称
        /// </summary>
        public string AppName
        {
            set
            {
                _appname = value;
            }
            get
            {
                return _appname;
            }
        }
        /// <summary>
        /// 安全令牌
        /// </summary>
        public string Token
        {
            set
            {
                _token = value;
            }
            get
            {
                return _token;
            }
        }
        /// <summary>
        /// 发起请求的消息类型编码
        /// </summary>
        public ActionType ActionType
        {
            set
            {
                _actiontype = value;
            }
            get
            {
                return _actiontype;
            }
        }

        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder xml = new StringBuilder();
                xml.Append(" <RequestData");
                xml.Append(" TransactionID=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.TransactionID )+ "\"");
                xml.Append(" RequestTime=\"" + this.RequestTime.ToString() + "\"");
                xml.Append(" AppName=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.AppName) + "\"");
                xml.Append(" Token=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Token )+ "\"");
                xml.Append(" ActionType=\"" + this.ActionType.ToString() + "\"");
                xml.Append(" >");

                return xml.ToString();
            }


        }
        public string XMLFinis
        {
            get
            {
                return "</RequestData>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            this.TransactionID = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["TransactionID"].InnerText);
            this.RequestTime = System.Convert.ToDateTime(this.CurrentXmlNode.Attributes["RequestTime"].InnerText);
            this.AppName = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["AppName"].InnerText);
            this.Token = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Token"].InnerText);
            string strActionType = this.CurrentXmlNode.Attributes["ActionType"].InnerText;
            switch (strActionType.ToLower())
            {
                case "audit_fetch":
                    this.ActionType = ActionType.AUDIT_FETCH;
                    break;
                case "dm_getdeleinfo":
                    this.ActionType = ActionType.DM_GETDELEINFO;
                    break;
                case "dm_getmodulelist":
                    this.ActionType = ActionType.DM_GETMODULELIST;
                    break;
                case "dm_setdeleinfo":
                    this.ActionType = ActionType.DM_SETDELEINFO;
                    break;
                case "iden_getacmodel":
                    this.ActionType = ActionType.IDEN_GETACMODEL;
                    break;
                case "iden_getlist":
                    this.ActionType = ActionType.IDEN_GETLIST;
                    break;
                case "iden_getuseraci":
                    this.ActionType = ActionType.IDEN_GETUSERACI;
                    break;
                case "iden_manage":
                    this.ActionType = ActionType.IDEN_MANAGE;
                    break;
                case "iden_setuseraci":
                    this.ActionType = ActionType.IDEN_SETUSERACI;
                    break;
                case "org_create":
                    this.ActionType = ActionType.ORG_CREATE;
                    break;
                case "org_delete":
                    this.ActionType = ActionType.ORG_DELETE;
                    break;
                case "org_update":
                    this.ActionType = ActionType.ORG_UPDATE;
                    break;
            }
            return this;
        }
        #endregion
    }
}
